//console.log("这里是快速仓库工具！"+window.location)
let flag_inventory_report = "/statistics/report-center/inventory-export-report-list/code/INVENTORY_EXPORT_REPORT"
let flag_orders_manage = "/order/order-list/list/platform"

/*
is_inventory_report = String(window.location).includes(flag_inventory_report)
if (is_inventory_report) {
    console.log("进入报表中心iframe")
}*/
/*
let is_orders_manage = String(window.location).includes(flag_orders_manage)
if (is_orders_manage) {
    console.log("进入订单管理iframe")
    let aEle = $('.orderDetail')
}
*/
//校验某个元素是否在某个数组里
function isInArray(ele, arr) {
    let ret = false
    for (let i = 0; i < arr.length; i++) {
        if (ele==arr[i]) {
            ret = true
        }
    }
    return ret
}
//通过添加再删除元素的方式把内容复制到剪贴板
function clipboardWrite(txt) {
    // 将文本内容复制到剪贴板
    a = document.createElement("input");
    a.setAttribute("id", "cp_hgz_input");
    a.value = txt;
    document.getElementsByTagName("body")[0].appendChild(a);
    document.getElementById("cp_hgz_input").select();
    document.execCommand("copy");
    document.getElementById("cp_hgz_input").remove();
    console.log('已把双击内容复制到剪贴板-'+txt)
}


//页面运行之前，注入hook
// 创建 script 元素
const script = document.createElement('script');
// 设置 script 元素的 src 属性
script.src = chrome.runtime.getURL('scripts/inject.js');
// 将 script 元素添加到页面中
(document.head || document.documentElement).appendChild(script);
//需要选中的仓库
repos = ["DGDREESMINE [DM_东莞]","DM_UK [DM_UK东莞]","BONITEE [Bo_东莞]","SZ01 [深圳仓]","DGEZBELLE [BE_东莞]"]

//接收响应消息
window.addEventListener("message",function(arg) {
    if (arg.data.type=="ECANG_ORDERS_MESSAGE_TYPE") {
        console.log("CS收到订单列表数据")
        // 为每个order#添加复制图标
        $('p.refrence_no.ellipsis.order_no_copy a').each(function(index, aEle) {
            let orderNo = $(aEle).text()//拿到订单号
            let parentP = $(aEle).parent()
            let spanObj = $("<span/>", {javascript:"void(0);", class:"copyWarehouseSku copyContent"})
            let inputObj = $("<input/>", {style:"opacity:0;cursor: pointer", type:"text", value:orderNo})//使用易仓自带的复制到剪贴板实现
            inputObj.appendTo(spanObj)
            spanObj.appendTo(parentP)
        })
        // 为每个参考号添加复制图标
        $('p.refrence_no.ellipsis:not(.order_no_copy)').each(function(index, aEle) {
            let orderStr = $(aEle).text() //e.g 参考号:281289798812
            let orderNo = orderStr.split(':')[1]//拿到订单号
            //let parentP = $(aEle).parent()
            let spanObj = $("<span/>", {javascript:"void(0);", class:"copyWarehouseSku copyContent"})
            let inputObj = $("<input/>", {style:"opacity:0;cursor: pointer", type:"text", value:orderNo})//使用易仓自带的复制到剪贴板实现
            inputObj.appendTo(spanObj)
            spanObj.appendTo(aEle)
        })
    }
    if (arg.data.type=="ECANG_INVENTORY_REPORT_MESSAGE_TYPE") {
        console.log("CS收到库存报表清单数据")
        //开始操作DOM
        sels = document.getElementsByTagName("select")
        //console.log(sels)
        if (sels && sels.length==8) {
            //实际甚至采购负责人及岑凌宇对应的值742
            sel1 = document.getElementsByName("operationType")
            sel11 = sel1[0]
            sel11.value = "buyer_id"
            sel2 = document.getElementsByName("person")
            sel21 = sel2[0]
            sel21.value = 742
            //设置产品负责人(显示框)
            divbox = document.getElementById("person_chosen")
            if (divbox) {
                alink = divbox.firstChild
                personspan = alink.firstChild
                personspan.innerHTML = "岑凌宇"
            }

            //选择仓库
            allinput = document.getElementsByTagName("input")
            for (let i = 0; i < allinput.length; i++) {
                let input = allinput[i]
                if (input.getAttribute("name")==="warehouse_arr[]") {
                    input.checked = false
                    let label = input.parentNode
                    let name = label.getAttribute("title")
                    if (isInArray(name, repos)) {
                        //console.log("第"+i+"个input:"+name)
                        input.checked = true
                    }
                }
            }
        }
    }
}, false)